require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsTaskBoxHandler ={};
local this = RedDotsTaskBoxHandler;
local taskConfig = nil;
local taskBoxConfig = nil;

function RedDotsTaskBoxHandler.init()
    if taskConfig == nil then
        taskConfig = G.dataTable["t_task"]
    end
    if taskBoxConfig == nil then
        taskBoxConfig =G.dataTable["t_taskbox"];
    end

    CloseEvent(Config.EventType.Fresh_TaskInfo,this.update);
    CloseEvent(Config.EventType.Res_TaskInfo_Get,this.update);

    ListenEvent(Config.EventType.Fresh_TaskInfo,this.update);
    ListenEvent(Config.EventType.Res_TaskInfo_Get,this.update);

    return this;
end

-- 参数1： 任务类型。nil代表 日常，周任务，成就全部检测
function RedDotsTaskBoxHandler.getFlag(...)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Task,false) then
        local taskTypes =nil;
        local param = ...;
        if param == nil then
            taskTypes = {1,2,4};
        else
            taskTypes ={param};
        end

        for key, value in pairs(taskBoxConfig) do
            local singleBoxCg = logicMgr.TaskManager.getTaskLivenessBoxServerData(value.f_BoxID);
            for k = 1,#taskTypes do
                if taskTypes[k] == value.f_BoxType and singleBoxCg.reward == 1 then
                    return Config.RedDotsFlagState.Needful;
                end
            end
        end
    end
    return Config.RedDotsFlagState.None;
end


function RedDotsTaskBoxHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.TaskBox);
end